package com.guigu.controller.manufacture;


import com.github.pagehelper.PageInfo;
import com.guigu.pojo.design.DFile;
import com.guigu.pojo.manufacture.MDesignProcedure;
import com.guigu.pojo.manufacture.MDesignProcedureDetails;
import com.guigu.service.design.DFileService;
import com.guigu.service.manufacture.MDesignProcedureDetailsService;
import com.guigu.service.manufacture.MDesignProcedureService;
import com.guigu.utils.CodeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr.Zhang
 * @since 2024-12-06
 */
@Controller
@RequestMapping("/m-design-procedure")
public class MDesignProcedureController {

    @Autowired
    private MDesignProcedureService mDesignProcedureService;

    @Autowired
    private MDesignProcedureDetailsService mDesignProcedureDetailsService;

    @Autowired
    private DFileService dFileService;

    // 制定产品生产工序设计单查询
    @GetMapping("/FormulateProductDesignQuery")
    public String formulateProductDesignQuery(){
        return "jsp/manufacture/b/Formulate_product_design_query";
    }
    //制定产品生产工序设计单-表格
    @GetMapping("/FormulateProductDesign")
    public String formulateProductDesign(){
        return "jsp/manufacture/b/Formulate_product_design";
    }
    //生产工序设计单审核
    @GetMapping("/ProductionProcessDesignSheet")
    public String productionProcessDesignSheet(){
        return "jsp/manufacture/b/Production_Process_Design_Sheet";
    }
    //产品生产工序设计单审核
    @GetMapping("/productProcessDesignReview")
    public String productProcessDesignReview(){
        return "jsp/manufacture/b/product_process_design_review";
    }
    //产品生产工序设计单查询页面
    @GetMapping("/InquiryProductDesignQuery")
    public String inquiryProductDesignQuery(){
        return "jsp/manufacture/b/Inquiry_product_design_query";
    }
    //产品生产工序设计单查询
    @GetMapping("/InquiryProductDesign")
    public String inquiryProductDesign(){
        return "jsp/manufacture/b/Inquiry_product_design";
    }
    //产品生产工序设计单变更
    @GetMapping("/ChangeProductDesign")
    public String changeProductDesign(){
        return "jsp/manufacture/b/Change_product_design";
    }
    //产品生产工序设计单变更查询页面
    @GetMapping("/ChangeProductDesignQeury")
    public String changeProductDesignQeury(){
        return "jsp/manufacture/b/Change_product_design_qeury";
    }
    //制定产品生产工序设计单
    @GetMapping("/FormulateProductDesignSheet")
    public String formulateProductDesignSheet(){
        return "jsp/manufacture/b/Formulate_product_design_sheet";
    }

    @GetMapping("/AddProductionProcessItems")
    public String addProductionProcessItems(){
        return "jsp/manufacture/b/Add_production_process_items";
    }

    @GetMapping("/ProductionProcessDesignAudit")
    public String productionProcessDesignAudit(){
        return "jsp/manufacture/b/Production_Process_Design_Audit";
    }

    @PostMapping("/EstablishProcess")
    @ResponseBody
    public Boolean EstablishProcess(@RequestBody ArrayList<Object> arr){
        System.out.println(arr);
        String productId = "";
        String moduleDescribe = "";
        String designer = "";
        String register = "";

        HashMap<String, Object> map = (HashMap<String, Object>) arr.get(0);
        if (map.containsKey("productId")) {
            productId = map.get("productId").toString();
        }
        if (map.containsKey("moduleDescribe")) {
            moduleDescribe = map.get("moduleDescribe").toString();
        }
        if (map.containsKey("designer")) {
            designer = map.get("designer").toString();
        }
        if (map.containsKey("register")) {
            register = map.get("register").toString();
        }

        DFile dFile = dFileService.selectByProductId(productId);
        BigDecimal costPrice = BigDecimal.valueOf(0L);
        ArrayList<MDesignProcedureDetails> details = new ArrayList<>();
        for (int i = 1; i < arr.size(); i++) {
            HashMap<String, Object> map1 = (HashMap<String, Object>) arr.get(i);
            MDesignProcedureDetails detail = new MDesignProcedureDetails();
            detail.setDetailsNumber(i);
            detail.setProcedureId(map1.get("procedureId").toString());
            detail.setProcedureName(map1.get("procedureName").toString());
            detail.setProcedureDescribe(map1.get("procedureDescribe").toString());
            detail.setLabourHourAmount(BigDecimal.valueOf(Long.parseLong(map1.get("labourHourAmount").toString())));
            detail.setAmountUnit(map1.get("amountUnit").toString());
            detail.setCostPrice(BigDecimal.valueOf(Long.parseLong(map1.get("costPrice").toString())));
            detail.setSubtotal(BigDecimal.valueOf(Long.parseLong(map1.get("subtotal").toString())));
            costPrice = BigDecimal.valueOf(Long.parseLong(costPrice.toString())+ Long.parseLong(detail.getSubtotal().toString()));
            detail.setRegister(register);
            detail.setRegisterTime(new Date());
            detail.setDesignModuleTag("0");
            detail.setDesignModuleChangeTag("0");
            details.add(detail);
        }
        MDesignProcedure procedure = new MDesignProcedure();
        MDesignProcedure endProcedure = mDesignProcedureService.getEndProcedure();
        CodeUtils codeUtils = new CodeUtils();
        String processCode = codeUtils.getProcessCode(endProcedure);

        procedure.setDesignId(processCode);
        procedure.setFirstKindId(dFile.getFirstKindId());
        procedure.setFirstKindName(dFile.getFirstKindName());
        procedure.setSecondKindId(dFile.getSecondKindId());
        procedure.setSecondKindName(dFile.getSecondKindName());
        procedure.setThirdKindId(dFile.getThirdKindId());
        procedure.setThirdKindName(dFile.getThirdKindName());
        procedure.setProductId(dFile.getProductId());
        procedure.setProductName(dFile.getProductName());
        procedure.setProcedureDescribe(moduleDescribe);
        procedure.setCostPriceSum(costPrice);
        procedure.setModuleCostPriceSum(BigDecimal.valueOf(0L));
        procedure.setDesigner(designer);
        procedure.setRegister(register);
        procedure.setRegisterTime(new Date());
        procedure.setCheckTag("0");
        procedure.setChangeTag("0");
        procedure.setDesignModuleTag("0");
        procedure.setDesignModuleChangeTag("0");

        int i = mDesignProcedureService.addProcedure(procedure);
        for (MDesignProcedureDetails detail : details) {
            detail.setParentId(procedure.getId());
            boolean save = mDesignProcedureDetailsService.save(detail);
        }
        dFile.setDesignProcedureTag("1");
        boolean b = dFileService.updateById(dFile);
        return b;
    }

    @PostMapping("/initialProduceTable")
    @ResponseBody
    public PageInfo<MDesignProcedure> initialProduceTable(@RequestBody HashMap<String, Object> map){
        PageInfo<MDesignProcedure> mDesignProcedurePageInfo = mDesignProcedureService.selectByPage(map);
        return mDesignProcedurePageInfo;
    }

    @PostMapping("/selectProductProcessById")
    @ResponseBody
    public MDesignProcedure selectProductProcessById(HttpServletRequest req){
        Integer id = StringUtils.isNotBlank(req.getParameter("id")) ? Integer.parseInt(req.getParameter("id")) : null;
        MDesignProcedure mDesignProcedure = mDesignProcedureService.selectById(id);
        return mDesignProcedure;
    }

    @GetMapping("/Audit")
    @ResponseBody
    public Boolean Audit(HttpServletRequest req){
        Integer id = StringUtils.isNotBlank(req.getParameter("id"))? Integer.parseInt(req.getParameter("id")) : null;
        MDesignProcedure mDesignProcedure = new MDesignProcedure();
        mDesignProcedure.setId(id);
        mDesignProcedure.setCheckTag("1");
        boolean b = mDesignProcedureService.updateById(mDesignProcedure);
        return b;
    }

    @GetMapping("/selectByproductId")
    @ResponseBody
    public MDesignProcedure selectByproductId(HttpServletRequest req){
        String productId = req.getParameter("productId");
        MDesignProcedure mDesignProcedure = mDesignProcedureService.selectByproductId(productId);
        return mDesignProcedure;
    }

}

